package com.wwu.common.minio;

import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * minio对象存储核心配置类
 * @author 一蓑烟雨
 * @version 1.0.0
 * @date 2022-09-02 12:00
 **/
@Configuration
public class MinioConfig {
    /** Minio服务地址 */
    @Value("${minio.apiEndpoint}")
    public String endpoint;
    /** 存储桶名称 */
    @Value("${minio.bucketName}")
    public String bucketName;
    /** 用户名 */
    @Value("${minio.accessKey}")
    public String accessKey;
    /** 密码 */
    @Value("${minio.secretKey}")
    public String secretKey;

    /**
     * 获取Minio客户端
     * @return: void
     * @author 一蓑烟雨
     * @date 2022/9/2 12:20
     */
    @Bean
    public MinioClient  minioClient() {
        MinioClient client = MinioClient.builder()
                .endpoint(endpoint)
                .credentials(accessKey, secretKey)
                .build();

        try {
            //判断存储桶是否存在,若不存在则创建
            if(!client.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build())){
                client.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  client;
    }
}
